package com.systop.party08.activity;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


import com.systop.party08.R;
import com.systop.party08.base.BaseActivity;
import com.systop.party08.entity.ArticleDetailEntity;
import com.systop.party08.http.MyStringCallback;
import com.systop.party08.utils.SPUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.systop.party08.http.ComUrl.ARTICLE_DETAIL;
import static com.systop.party08.http.ComUrl.ComUrl;
import static com.systop.party08.utils.StaticDateUtils.SP_TOKEN;


public class ArticleDetailActivity extends BaseActivity {

    @BindView(R.id.detail_wv)
    WebView wv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_article_detail);
        ButterKnife.bind(this);
        setIvBack();
        setTvTitle("详情");
        initWebView();
       // okhttp();
    }

    private void initWebView() {
        //webview的配置
        WebSettings setting = wv.getSettings();
        setting.setJavaScriptEnabled(true);
        setting.setJavaScriptCanOpenWindowsAutomatically(true);
        setting.setUseWideViewPort(true);//关键点
        setting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);// 排版适应屏幕
        setting.setSupportZoom(true);//支持缩放
        setting.setBuiltInZoomControls(true);
        setting.setAppCacheEnabled(true);
        setting.setDisplayZoomControls(false);//显示缩放按钮
        setting.setBlockNetworkImage(false);//禁止加载网络图片
        setting.setAllowFileAccess(true);//允许访问文件
        setting.setTextSize(WebSettings.TextSize.NORMAL);
//        setting.setTextZoom(150);
        setting.setDefaultTextEncodingName("UTF -8");
        setting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        setting.setLoadWithOverviewMode(true);//适配时很重要
        setting.setDomStorageEnabled(true);

        wv.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                wv.loadUrl(url);
                return true;
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        wv.stopLoading();
        wv.removeAllViews();
        wv.destroy();
        wv = null;
    }

    private void okhttp() {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", SPUtils.getPreference(this, SP_TOKEN));
        param.put("artId", getIntent().getStringExtra("id"));
      //  param.put("lx", getIntent().getStringExtra("type"));//wsdx党员文章/wz 普通文章（首页，网上党校）/tz最新通知
        commonOkhttp.execute(ARTICLE_DETAIL, param, new MyStringCallback(this) {
            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);
                ArticleDetailEntity entity = gson.fromJson(response, ArticleDetailEntity.class);
                if (entity != null && entity.getData() != null) {
                    ArticleDetailEntity.DataBean dataBean = entity.getData();
                    String url = dataBean.getUrl();
                    wv.loadUrl(ComUrl + url);
                }
            }
        });
    }


}